package java6703.task;

import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

//控制器类
public class NoRepeatStudent6703Controller {
    @FXML
    private TextField tfName;

    @FXML
    private TextField tfAge;

    @FXML
    private ListView<Student6703> lvStudent;

    @FXML
    void addClick(ActionEvent event) {
        String name = tfName.getText().replaceAll(" +", "");
        String age = tfAge.getText().replaceAll(" +", "");
        //判断name和age是否都为空，年龄是否符合
        if (isNull(name, age) || !age.matches("\\d{1,3}")) {
            if ("".equals(name)) {
                alert("请输入姓名");
            }
            if ("".equals(age)) {
                alert("请输入年龄");
            } else if (!age.matches("\\d{1,3}")) {
                alert("请输入合法年龄");
            }
        } else {
            //获取当前编辑框中的姓名和年龄插入到ListView中
            ObservableList<Student6703> items = lvStudent.getItems();
            Student6703 student6703 = new Student6703(name, Integer.parseInt(age));
            items.add(student6703);
        }
    }

    @FXML
    void changeClick(ActionEvent event) {
        String name = tfName.getText().replaceAll(" +", "");
        String age = tfAge.getText().replaceAll(" +", "");
        //判断name和age是否都为空，年龄是否符合
        if (isNull(name, age) || !age.matches("\\d{1,3}")) {
            if ("".equals(name)) {
                alert("请输入姓名");
            }
            if ("".equals(age)) {
                alert("请输入年龄");
            } else if (!age.matches("\\d{1,3}")) {
                alert("请输入合法年龄");
            }
        } else {
            //获取当前编辑框中的姓名和年龄插入到ListView中
            ObservableList<Student6703> items = lvStudent.getItems();
            int selectedIndex = lvStudent.getSelectionModel().getSelectedIndex();
            Student6703 student6703 = new Student6703(name, Integer.parseInt(age));
            items.remove(selectedIndex);
            items.add(selectedIndex, student6703);
            lvStudent.getSelectionModel().select(selectedIndex);
        }
    }

    @FXML
    void clearClick(ActionEvent event) {
        ObservableList<Student6703> items = lvStudent.getItems();
        //通过TreeSet筛选重复项
        TreeSet<Student6703> student6703s = new TreeSet<Student6703>();
        for (int i = 0; i < items.size(); i++) {
            student6703s.add(items.get(i));
        }
        items.setAll(student6703s);
        lvStudent.getSelectionModel().select(0);
    }

    @FXML
    void initialize() {
        //初始化Student6703对象
        ObservableList<Student6703> items = lvStudent.getItems();
        List<Student6703> students = new ArrayList<Student6703>() {
            {
                add(new Student6703("李Java", 21));
                add(new Student6703("陈Python", 18));
                add(new Student6703("牛C++", 31));
                add(new Student6703("马JS", 21));
                add(new Student6703("陈Java", 21));
                add(new Student6703("马JS", 21));
            }
        };
        items.addAll(students);
        lvStudent.getSelectionModel().select(0);
        Student6703 selectedItem = lvStudent.getSelectionModel().getSelectedItem();
        tfName.setText(selectedItem.getName());
        tfAge.setText(String.valueOf(selectedItem.getAge()));
        lvStudent.getSelectionModel().selectedItemProperty().addListener(
                (observable, oldValue, newValue) -> {
                    if (newValue != null) {
                        tfName.setText(newValue.getName());
                        tfAge.setText(String.valueOf(newValue.getAge()));
                    }
                });
    }

    //提取提示窗口方法
    void alert(String al) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("ListView与对象集合");
        alert.setHeaderText("请注意");
        alert.setContentText(al);
        alert.show();
    }

    public boolean isNull(String name, String age) {
        if ("".equals(name) || "".equals(age)) {
            return true;
        }
        return false;
    }
}
